package ru.nlezhnev.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class Database {
	private static final Database instance = new Database();
	
	private DataSource dataSource;

	private Database() {
		try {
			Context ctx = new InitialContext();

			dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/FarmDB");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Database getInstance() {
		return instance;
	}
	
	synchronized public Connection getConnection() throws SQLException {
		Connection conn = dataSource.getConnection();
		return conn;
	}
}